﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Microsoft.SqlServer.Management;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using Hafic.Stock.Management.Common;

namespace DXEasyAccount.Security.Forms
{
    public partial class frmRestore : DevExpress.XtraEditors.XtraForm
    {
        #region "Define Variable"
        Server srv;
        ServerConnection conn;
        private string _strfileName;

        private string _sqlServerInstance;
        private string _sqlDatabaseName;
        private string _sqlUserName;
        private string _sqlPassword;
        private string[] arrSqlServer;
        #endregion

        #region "Construction"
        public frmRestore()
        {
            InitializeComponent();
        }
        #endregion

        #region "Private Method"
        private void doRestore()
        {
            conn = new ServerConnection(_sqlServerInstance, _sqlUserName, _sqlPassword);
            srv = new Server(conn);

            Restore res = new Restore();

            this.Cursor = Cursors.WaitCursor;
            _strfileName = String.Format(@"{0}", txtFileName.EditValue);

            res.Database = _sqlDatabaseName;
            res.Action = RestoreActionType.Database;
            res.Devices.AddDevice(_strfileName, DeviceType.File);
            this.processBar.EditValue = 0;
            this.processBar.Properties.Maximum = 100;
            this.processBar.EditValue = 10;
            res.PercentCompleteNotification = 10;
            res.ReplaceDatabase = true;
            res.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);            
            try
            {
                res.SqlRestore(srv);
                XtraMessageBox.Show(
                    "Khôi phục dữ liệu thành công từ tệp " + _strfileName, 
                    "Thông tin");
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
            finally
            {
                this.Cursor = Cursors.Default;
                this.processBar.EditValue = 0;
            }

        }
        #endregion

        #region "Form Event"
        private void frmRestore_Load(object sender, EventArgs e)
        {
            arrSqlServer = clsVariable.strSqlConnectionString.Split(';');
            _sqlServerInstance = arrSqlServer[0].Replace("Data Source", "").Replace("=", "").Trim();
            _sqlDatabaseName = arrSqlServer[1].Replace("Initial Catalog", "").Replace("=", "").Trim();
            _sqlUserName = arrSqlServer[2].Replace("User Id", "").Replace("=", "").Trim();
            _sqlPassword = arrSqlServer[3].Replace("Password", "").Replace("=", "").Trim();

            txtDatabase.Text = _sqlDatabaseName;
        }
        #endregion

        #region "Process Bar Event"
        public void ProgressEventHandler(object sender, PercentCompleteEventArgs e)
        {
            this.processBar.EditValue = e.Percent;
        }

        #endregion

        #region "Button Event"
        private void txtFolder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            openFileDialog.ShowDialog();
            if (!String.IsNullOrEmpty(openFileDialog.FileName))
            {
                txtFileName.EditValue = openFileDialog.FileName;
            }
        }

        
        private void btnOk_Click(object sender, EventArgs e)
        {
            doRestore();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}